package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.SparePartTransactionRecordDao;
import com.sugon.entity.*;
import com.sugon.modules.sparepart.consts.SparePartTransactionType;
import com.sugon.service.SparePartService;
import com.sugon.service.SparePartTransactionRecordService;
import com.sugon.service.SpareStorePosService;
import com.sugon.service.StoreHouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 备件事务记录Service实现类
 *
 * @author sugon
 * @date 2022-09-05 21:00:50
 */
@Service("sparePartTransactionRecordService")
public class SparePartTransactionRecordServiceImpl implements SparePartTransactionRecordService {

    @Resource
    private SparePartTransactionRecordDao sparePartTransactionRecordDao;
    @Resource
    private SparePartService sparePartService;
    @Resource
    private StoreHouseService storeHouseService;
    @Resource
    private SpareStorePosService spareStorePosService;

    @Override
    public boolean generateOutTransaction(SparePartOutEntity out) {
        // 获取备件详情
        SparePartEntity spareParts = sparePartService.queryByCode(out.getPartCode());
        Assert.notNull(spareParts);
        // 先删除
        sparePartTransactionRecordDao.conditionDelete(SparePartTransactionType.OUT, out.getId());
        // 创建事务记录
        SparePartTransactionRecordEntity record = new SparePartTransactionRecordEntity();
        record.setTransactionType(SparePartTransactionType.OUT);
        record.setBusId(out.getId());
        record.setBusOrderNo(out.getOutCode());
        record.setCargoLocation(out.getPostionId());
        record.setSparePartId(spareParts.getId());
        record.setQuantity(out.getOutNum() - out.getBackNum());
        record.setOperator(out.getCreateUser());
        record.setOperateDate(out.getCreateDate());
        record.setTransactionDesc(StrUtil.format("出库位置：[{}({})]", out.getOutStore(), out.getCargoLocation()));
        return save(record) > 0;
    }

    @Override
    public boolean generateConsumeTransaction(SparePartConsumRecordEntity consumeRecord) {
        // 获取备件详情
        SparePartEntity spareParts = sparePartService.queryObject(consumeRecord.getSparePartId());
        Assert.notNull(spareParts);
        // 先删除
        sparePartTransactionRecordDao.conditionDelete(SparePartTransactionType.CONSUME, consumeRecord.getId());
        // 创建事务记录
        SparePartTransactionRecordEntity record = new SparePartTransactionRecordEntity();
        record.setQuantity(consumeRecord.getNum());
        record.setTransactionType(SparePartTransactionType.CONSUME);
        record.setBusId(consumeRecord.getId());
        record.setBusOrderNo(consumeRecord.getId());
        record.setCargoLocation(consumeRecord.getPosId());
        record.setSparePartId(spareParts.getId());
        record.setOperator(consumeRecord.getCreateUser());
        record.setOperateDate(consumeRecord.getGmtCreate());
        record.setTransactionDesc(StrUtil.format("车间内部使用{}[{}]，数量：{}", spareParts.getName(), spareParts.getSpec(), consumeRecord.getNum()));
        return save(record) > 0;
    }

    @Override
    public boolean generateInTransaction(SparePartInEntity in) {
        // 获取备件详情
        SparePartEntity spareParts = sparePartService.queryByCode(in.getPartCode());
        Assert.notNull(spareParts);
        // 先删除
        sparePartTransactionRecordDao.conditionDelete(SparePartTransactionType.IN, in.getId());
        // 创建事务记录
        SparePartTransactionRecordEntity record = new SparePartTransactionRecordEntity();
        record.setTransactionType(SparePartTransactionType.IN);
        record.setBusId(in.getId());
        record.setBusOrderNo(in.getInCode());
        record.setCargoLocation(in.getCargoLocation());
        record.setSparePartId(spareParts.getId());
        record.setQuantity(in.getInNum());
        record.setOperator(in.getCreateUser());
        record.setOperateDate(in.getCreateDate());
        // 设置事务描述
        StoreHouseEntity storeHouse = storeHouseService.queryObject(in.getInStoreId());
        SpareStorePosEntity spareStorePos = spareStorePosService.queryObject(in.getCargoLocation());
        if (storeHouse != null && spareStorePos != null) {
            record.setTransactionDesc(StrUtil.format("入库位置：[{}({})]", storeHouse.getName(), spareStorePos.getPostion()));
        }
        return save(record) > 0;
    }

    @Override
    public boolean generateBackTransaction(SparePartBackEntity back) {
        // 获取备件详情
        SparePartEntity spareParts = sparePartService.queryByCode(back.getPartCode());
        Assert.notNull(spareParts);
        // 先删除
        sparePartTransactionRecordDao.conditionDelete(SparePartTransactionType.BACK, back.getId());
        // 创建事务记录
        SparePartTransactionRecordEntity record = new SparePartTransactionRecordEntity();
        record.setTransactionType(SparePartTransactionType.BACK);
        record.setBusId(back.getId());
        record.setBusOrderNo(back.getOutCode());
        record.setCargoLocation(back.getPostionId());
        record.setSparePartId(spareParts.getId());
        record.setQuantity(back.getBackNum());
        record.setOperator(back.getCreateUser());
        record.setOperateDate(back.getCreateDate());
        // 设置事务描述
        SpareStorePosEntity spareStorePos = spareStorePosService.queryObject(record.getCargoLocation());
        if (spareStorePos != null) {
            record.setTransactionDesc(StrUtil.format("退库到：[{}({})]", back.getOutStore(), spareStorePos.getPostion()));
        } else {
            record.setTransactionDesc(StrUtil.format("退库到：[{}]", back.getOutStore()));
        }
        return save(record) > 0;
    }

    @Override
    public SparePartTransactionRecordEntity queryObject(String id) {
        return sparePartTransactionRecordDao.queryObject(id);
    }

    @Override
    public List<SparePartTransactionRecordEntity> queryList(Map<String, Object> map) {
        return sparePartTransactionRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sparePartTransactionRecordDao.queryTotal(map);
    }

    @Override
    public int save(SparePartTransactionRecordEntity sparePartTransactionRecord) {
        sparePartTransactionRecord.setId(RandomUtil.randomString(32));
        sparePartTransactionRecord.setGmtCreate(DateUtil.date());
        // 入库和退库为正数，出库和备件消耗需要调整为负数
        if (StrUtil.equalsAny(sparePartTransactionRecord.getTransactionType(), SparePartTransactionType.OUT, SparePartTransactionType.CONSUME)) {
            sparePartTransactionRecord.setQuantity(-1 * sparePartTransactionRecord.getQuantity());
        }
        return sparePartTransactionRecordDao.save(sparePartTransactionRecord);
    }

    @Override
    public int update(SparePartTransactionRecordEntity sparePartTransactionRecord) {
        // 入库和退库为正数，出库和备件消耗需要调整为负数
        if (StrUtil.equalsAny(sparePartTransactionRecord.getTransactionType(), SparePartTransactionType.OUT, SparePartTransactionType.CONSUME)) {
            sparePartTransactionRecord.setQuantity(-1 * sparePartTransactionRecord.getQuantity());
        }
        return sparePartTransactionRecordDao.update(sparePartTransactionRecord);
    }

    @Override
    public int delete(String id) {
        return sparePartTransactionRecordDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return sparePartTransactionRecordDao.deleteBatch(ids);
    }

}
